/*
// 用于 ..need_title/
filters=(
    r"[_\.\-\[\]/]",
    r"[,;]",
    r"[【】《》（）]",
    r"[hx]26[45]",
    r"mkv",
    r"mp4",
    r"aac",
    r"hevc",
    r"avc",
    r"蓝光",
    r"blueray",
    r"高清",
    r"\\",
    r"  +"
)

// 用于 ..need_title2/

filters2=(
    r"[_\.\-\[\]/]",
    r"[,;]",
    r"[【】《》（）]",
    r"\\",
    r"  +"
)

*/

//// 必须过滤, 不然拼接  格式会错错乱

package walker

import (
	"fmt"
	"regexp"
	"strconv"
	"strings"
)

var (
	compiled_reg, _  = regexp.Compile(`(?i)[_\.\-\[\]/\\,;:【】《》（）：!@#\$%\^\&\*\=\?\<\>]|[hx]26[45]|mkv|rmvb|mp4|aac|hevc|dvd|avc|蓝光|blueray|高清|  +`)
	compiled_reg2, _ = regexp.Compile(`(?i) +`)
)

func titleFilterX(filter int, name string) string {
	if filter == 0 {
		// return ""
		filter = 1 //默认使用 filter 1
	}
	name = strings.ReplaceAll(name, "#", " ")
	name = strings.ReplaceAll(name, "$", " ")
	if filter == 1 {
		name = compiled_reg.ReplaceAllString(name, " ")
	}
	name = compiled_reg2.ReplaceAllString(name, " ")
	name = strings.Trim(name, " ")
	// switch filter {
	// case 0:
	// 	return ""
	// case 1:
	// 	//todo

	// 	return name
	// case 2:
	// 	return name
	// }
	return name
}

// 根据视频  添加 标题: 第几部-name
func videoTitleN(path string, name string) (string, int) {
	if i := strings.LastIndex(path, "?"); i >= 0 {
		path = path[:i]
	}
	path = strings.TrimRight(path, "/")

	if i := strings.LastIndex(path, "/"); i >= 0 {
		//  /已trim所不会是 最后一个字符
		path = path[i+1:]
	}
	if len(path) == 0 {
		return name, -1
	}

	s := path
	if !strings.HasPrefix(s, "0000000000") {
		return name, -1
	}

	/// 获取 名字 : "第%d部"
	p := strings.TrimLeft(path, "0")
	if !strings.HasPrefix(p, "x") {
		return name, -1
	}

	i := strings.Index(p, ".")
	if i < 0 {
		return name, -1
	}
	p = p[1:i]
	i, err := strconv.Atoi(p)
	if err != nil {
		return name, -1
	}
	if i < 1 || i > 99 {
		return name, -1
	}

	return fmt.Sprintf("第%d部", i), i

}
